import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/bloc/SelectUsersCubit.dart';
import 'package:im_app/im/page/sub/GroupSelectPage.dart';
import 'package:im_app/im/page/bloc/GroupAddCubit.dart';
import 'package:im_app/im/util/ImConstant.dart';
import '../../util/HexColor.dart';
import '../view/SelectUsersView.dart';

class GroupAddPage extends StatefulWidget {
  const GroupAddPage({super.key});

  @override
  State<StatefulWidget> createState() => _GroupAddState();
}

class _GroupAddState extends State<GroupAddPage> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<GroupAddCubit, GroupAddState>(
      listener: (BuildContext context, GroupAddState groupAddState) {
        var router = groupAddState.forwardRouter;
        if (router == '/MessageSendPage') {
          Navigator.of(context).pushNamedAndRemoveUntil("/MessageSendPage", ModalRoute.withName('/MainPage'),
              arguments: {"id": groupAddState.forwardSession?.id!, "name": groupAddState.forwardSession?.name});
        }
      },
      builder: (BuildContext context, GroupAddState state) {
        var groupAddCubit = context.watch<GroupAddCubit>();
        return Scaffold(
          appBar: _buildAppbar(context),
          backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
          body: SafeArea(
            left: false,
            right: false,
            child: Column(
              children: [
                Expanded(child: _buildBody(context, groupAddCubit)),
                _buildBottomControlPane(context),
              ],
            ),
          ),
        );
      },
    );
  }

  AppBar _buildAppbar(BuildContext context) {
    return AppBar(
      leading: InkWell(
        child: const Icon(Icons.arrow_back),
        onTap: () {
          var selectUsersCubit = context.read<SelectUsersCubit>();
          selectUsersCubit.add(new SelectUsersResetEvent());
          Navigator.of(context).pop();
        },
      ),
      title: const Text(
        "发起群聊",
        style: TextStyle(fontSize: ImConstant.IM_DEFAULT_SIZE, fontWeight: FontWeight.w500),
      ),
      toolbarHeight: ImConstant.IM_APP_BAR_HEIGTH,
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      centerTitle: true,
    );
  }

  Widget _buildBody(BuildContext context, GroupAddCubit groupAddCubit) {
    return SelectUsersView(groupWidget: InkWell(
      child: const ListTile(
        title: Text('选择一个群'),
        dense: true,
      ),
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) {
          return const GroupSelectPage();
        }));
      },
    ));
  }

  Widget _buildBottomControlPane(BuildContext context) {
    var groupAddCubit = context.read<GroupAddCubit>();
    var selectUsersCubit = context.read<SelectUsersCubit>();
    return Container(
      decoration: BoxDecoration(
          color: ImConstant.IM_MAIN_BACKGROUND_COLOR,
          border: Border(top: BorderSide(color: HexColor.fromHex("#EEEDED"), width: 1))
      ),
      padding: const EdgeInsets.only(top: 10, bottom: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          TextButton(
            child: const Text('保存'),
            onPressed: () {
              groupAddCubit.add(GroupAddSaveEvent(selectUsersCubit.state.selectedUsers));
            },
          ),
          TextButton(
            child: const Text('取消'),
            onPressed: () {
              selectUsersCubit.add(SelectUsersCancelEvent());
            },
          )
        ],
      ),
    );
  }

}
